package com.timguan.render.dataprocessor.filter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.timguan.render.dataapi.domain.CmsDataDO;
import com.timguan.render.dataprocessor.processor.DataProcessor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class SoldoutProductFilter4Tab implements DataFilter {
    private static final Logger logger = LoggerFactory.getLogger(SoldoutProductFilter4Tab.class);
    @Autowired
    private SoldoutProductFilter soldoutProductFilter;

    @Override
    public List<CmsDataDO> doFilter(List<CmsDataDO> cmsDataDOList, Map<String, String> params) {
        List<CmsDataDO> result = null;
        if (cmsDataDOList == null) {
            return null;
        }
        int size = params != null && StringUtils.isNotBlank(params.get(DataProcessor.MAX_COUNT_PARAM)) &&
                StringUtils.isNumeric(params.get(DataProcessor.MAX_COUNT_PARAM)) ? Integer.parseInt(params.get(DataProcessor.MAX_COUNT_PARAM)) : 0;
        if (size > 0) {
            Map<String, List<CmsDataDO>> cmsDataGroupByTabs = groupByTag(cmsDataDOList);
            if (cmsDataGroupByTabs != null) {
                result = new ArrayList<CmsDataDO>();
                for (List<CmsDataDO> cmsDataDOs : cmsDataGroupByTabs.values()) {
                    result.addAll(soldoutProductFilter.doFilter(cmsDataDOs, params));
                }
            }
        } else {
            result = cmsDataDOList;
        }
        return result;
    }

    /**
     * 数据按照tag进行聚合
     *
     * @param cmsDataDOList
     * @return
     */
    private static Map<String, List<CmsDataDO>> groupByTag(List<CmsDataDO> cmsDataDOList) {
        Map<String, List<CmsDataDO>> cmsDataGroupByTabs = null;
        if (cmsDataDOList != null && cmsDataDOList.size() > 0) {
            cmsDataGroupByTabs = new LinkedHashMap<String, List<CmsDataDO>>();
            for (CmsDataDO data : cmsDataDOList) {
                try {
                    String tag = null;
                    Map<String, String> dataEntry = JSON.parseObject(data.getPropertyValue(), new TypeReference<Map<String, String>>() {
                    });
                    if (dataEntry != null) {
                        if (StringUtils.isNotBlank(dataEntry.get(DataProcessor.TAG))) {
                            tag = dataEntry.get(DataProcessor.TAG);
                        }
                        List<CmsDataDO> cmsDataDOs;
                        if (cmsDataGroupByTabs.get(tag) != null) {
                            cmsDataDOs = cmsDataGroupByTabs.get(tag);
                        } else {
                            cmsDataDOs = new ArrayList<CmsDataDO>();
                            cmsDataGroupByTabs.put(tag, cmsDataDOs);
                        }
                        cmsDataDOs.add(data);
                    }
                } catch (Exception e) {
                    logger.error("group by tag failed!", e);
                }
            }
        }
        return cmsDataGroupByTabs;
    }
}